const userService = require("../services/user");
// const { validateLogin } = require("../validator/user"); // 参数校验

exports.getUserInfo = async (ctx) => {
    const { page, pageSize } = ctx.request.body || {};
    console.log("kkkkkkk", page, pageSize);

    const { userList } = await userService.getUserInfo({
        page,
        pageSize
    });
    // 3. 返回响应
    ctx.body = { code: "00", data: userList };
};

/**
 * 登录
 * @param {*} ctx
 */
exports.login = async (ctx) => {
    let code = ctx.request.body.code;
    console.log("code", code);
    if (!code) {
        ctx.status = 400;
        ctx.body = { code: 400, message: "缺少code参数" };
        return;
    }
    const { token, openid } = await userService.login({ code });

    ctx.body = {
        code: "00",
        data: {
            token,
            userInfo: {
                openid
                // createdAt: user.createdAt
            }
        },
        message: "登录成功"
    };
};
/**
 * 刷新token
 * @param {*} ctx
 */
exports.refreshToken = async (ctx) => {
    let openid = ctx.request.body.openid;
    if (!openid) {
        ctx.status = 400;
        ctx.body = { code: 400, message: "缺少openid参数" };
        return;
    }

    const { token } = await userService.refreshToken({
        openid
    });

    ctx.body = {
        code: "00",
        data: {
            token
        },
        message: "token获取成功"
    };
};
